package com.core.zyyz.pcmc.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.core.common.utils.StringUtils;
import com.core.zyyz.pcmc.domain.PcmcRoleUser;
import com.core.zyyz.pcmc.domain.PcmcUser;
import com.core.zyyz.pcmc.mapper.PcmcRoleMapper;
import com.core.zyyz.pcmc.domain.PcmcRole;
import com.core.zyyz.pcmc.mapper.PcmcRoleUserMapper;
import com.core.zyyz.pcmc.service.PcmcRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * 角色Service业务层处理
 *
 * @author core
 * @date 2024-10-15
 */
@Service
@Transactional
public class PcmcRoleServiceImpl implements PcmcRoleService {

    @Autowired
    private PcmcRoleMapper pcmcRoleMapper;
    @Autowired
    private PcmcRoleUserMapper pcmcRoleUserMapper;

    /**
     * 查询角色
     *
     * @param roleCode 角色主键
     * @return 角色
     */
    @Override
    public PcmcRole selectPcmcRoleByRoleCode(String roleCode) {
        return pcmcRoleMapper.selectPcmcRoleByRoleCode(roleCode);
    }

    /**
     * 查询角色列表
     *
     * @param pcmcRole 角色
     * @return 角色
     */
    @Override
    public List<PcmcRole> selectPcmcRoleList(PcmcRole pcmcRole) {
        return pcmcRoleMapper.selectPcmcRoleList(pcmcRole);
    }

    /**
     * 新增角色
     *
     * @param pcmcRole 角色
     * @return 结果
     */
    @Override
    public int insertPcmcRole(PcmcRole pcmcRole) throws Exception {
        PcmcRole role = pcmcRoleMapper.selectPcmcRoleByRoleCode(pcmcRole.getUserCode());
        if (role != null){
            throw new Exception("角色编号已存在！");
        }else {
            return pcmcRoleMapper.insertPcmcRole(pcmcRole);
        }
    }

    /**
     * 修改角色
     *
     * @param pcmcRole 角色
     * @return 结果
     */
    @Override
    public int updatePcmcRole(PcmcRole pcmcRole) {
        return pcmcRoleMapper.updatePcmcRole(pcmcRole);
    }

    /**
     * 批量删除角色
     *
     * @param roleCodes 需要删除的角色主键
     * @return 结果
     */
    @Override
    public int deletePcmcRoleByRoleCodes(String[] roleCodes) {
        return pcmcRoleMapper.deletePcmcRoleByRoleCodes(roleCodes);
    }

    /**
     * 删除角色信息
     *
     * @param roleCode 角色主键
     * @return 结果
     */
    @Override
    public int deletePcmcRoleByRoleCode(String roleCode) {
        return pcmcRoleMapper.deletePcmcRoleByRoleCode(roleCode);
    }

    /**
     * 查询角色未授权用户
     * @return
     */
    public  List<PcmcUser> queryRoleUnGrantUserList() {
       return pcmcRoleUserMapper.queryRoleUnGrantUserList();
    }

    /**
     * 查询角色已授权用户
     * @param param
     * @return
     */
    public  List<PcmcUser> queryRoleGrantUserList(PcmcUser param) {
        return pcmcRoleUserMapper.queryRoleGrantUserList(param);
    }

    /**
     * 角色授权用户
     *
     * @param pcmcRoleUser
     */
    @Override
    @Transactional
    public void insertUserAuth(PcmcRoleUser pcmcRoleUser) {
        String roleCode = pcmcRoleUser.getRoleCode();
        String[] userCodes = pcmcRoleUser.getUserCodes();
        pcmcRoleUserMapper.deleteUserRoleByRoleCode(roleCode);
//        PcmcUser user = (PcmcUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();

        if (StringUtils.isNotEmpty(userCodes)) {
            // 新增用户与角色管理
            List<PcmcRoleUser> list = new ArrayList<PcmcRoleUser>(userCodes.length);
            for (String userCode : userCodes) {
                PcmcRoleUser roleUser = new PcmcRoleUser();
                roleUser.setUserCode(userCode);
                roleUser.setRoleCode(roleCode);
                roleUser.setCreateBy("admin");
                roleUser.setCreateDate(new Date());
                roleUser.setLastUpdateBy("admin");
                roleUser.setLastUpdateDate(new Date());
                list.add(roleUser);
            }
            pcmcRoleUserMapper.batchRoleUser(list);
        }
    }

}
